/**
 * @author zhj
 * @description mongoose操作类(封装mongodb)
 */

var mongoose = require("mongoose");
var logger = require("pomelo-logger").getLogger("mongodb-log");
var confg = require("../config");

var dbURL =
  "mongodb://" +
  confg.db_user +
  ":" +
  confg.db_pwd +
  "@" +
  confg.db_host +
  ":" +
  confg.db_port +
  "/" +
  confg.db_name;

console.log(dbURL, "链接地址");

const options = {
  user: "ubuntu",
  pass: "z12h14j.",
  auth: { authMechanism: "MONGODB-CR" },
  useUnifiedTopology: true,
  useNewUrlParser: true
};
// confg.local  dbURL
mongoose.connect(dbURL, { useUnifiedTopology: true, useNewUrlParser: true }, err => {
// mongoose.connect("mongodb://@118.25.177.80:27017/zhj1214", options, err => {
  if (err) {
    console.error("Failed to connect to database 未能连接到数据库");
  } else {
    console.log("Connecting database successfully 连接数据库成功");
  }
});

mongoose.connection.on("connected", function(err) {
  if (err) logger.error("Database connection failure 数据库连接失败");
});

mongoose.connection.on("error", function(err) {
  logger.error("Mongoose connected error " + err);
});

mongoose.connection.on("disconnected", function() {
  logger.error("Mongoose disconnected 数据库断开连接");
});

process.on("SIGINT", function() {
  mongoose.connection.close(function() {
    logger.info(
      "Mongoose disconnected through app termination 数据库通过终止应用程序断开连接"
    );
    process.exit(0);
  });
});
